#include "timeStamp.h"
#include "public.h"
#include <ctime>
#include <iostream>

TimeStamp::TimeStamp() : m_secondSinceEpoch(0) {}
TimeStamp::TimeStamp(int64_t secondSinceEpoch)
    : m_secondSinceEpoch(secondSinceEpoch) 
    {}

// secondSinceEpoch -> localtime -> struct tm  -> std::string  转换思路流程
std::string TimeStamp::ToString(){
    struct tm t;
    localtime_r(&m_secondSinceEpoch, &t);

    char buf[SMALL_BUF_LEN] = {0};
    snprintf(buf, SMALL_BUF_LEN, "[%04d/%02d/%02d %02d:%02d:%02d]",
    t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
    //snprintf格式化输出

    return buf;
}

// time
TimeStamp TimeStamp::Now() {
    return TimeStamp(time(NULL));
    //用time(NULL)返回的秒数，构造对象
}


// int main()
// {
//     std::cout<<TimeStamp::Now().ToString() <<std::endl;
//     //TimeStamp t;
//     //std::cout<<t.Now().ToString() <<std::endl;

//     return 0;
// }